package com.blog.domain;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ArticleMeta extends POJO {

	private static final long serialVersionUID = 3561646756856890359L;

	//�ֶ�
	protected String title;
	protected String stitle;
	protected String ltitle;
	protected String author;
	protected String time;
	protected int ops;
	
	@Override
	public String getTable() {
		return "articlemeta";
	}

	@Override
	protected Map<String, Object> listFields() {
		Map<String,Object> fields = super.listFields();
		fields.remove("formatTime");
		return fields;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getStitle() {
		return stitle;
	}

	public void setStitle(String stitle) {
		this.stitle = stitle;
	}

	public String getLtitle() {
		return ltitle;
	}

	public void setLtitle(String ltitle) {
		this.ltitle = ltitle;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getTime() {
		return time;
	}

	public String getFormatTime(){
		if( time != null ){
			try{
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date = sdf.parse(time);
				sdf.applyPattern("yyyy-MM-dd");
				return sdf.format(date);
			}catch (ParseException e){
				e.printStackTrace();
			}
		}
		return time;
	}

	public String year(){
		return time.split("-")[0];
	}

	public String month(){
		return time.split("-")[1];
	}

	public Map<String,Map<String,List<ArticleMeta>>> sortByTime(List<ArticleMeta> metas){
		final Map<String,Map<String,List<ArticleMeta>>> sortedMetas = Maps.newLinkedHashMap();
		Comparator<ArticleMeta> comparator = new Comparator<ArticleMeta>() {
			@Override
			public int compare(ArticleMeta o1, ArticleMeta o2) {
				return -o1.time.compareTo(o2.time);
			}
		};
		List<ArticleMeta> sorted = Ordering.from(comparator).sortedCopy(metas);
		for( ArticleMeta articleMeta : sorted ) {
				String year = articleMeta.year();
				String month = articleMeta.month();
				if (sortedMetas.containsKey(year)) {
					if (!sortedMetas.get(year).containsKey(month)) {
						Map<String, List<ArticleMeta>> tmp = sortedMetas.get(year);
						List<ArticleMeta> ms = Lists.newArrayList();
						ms.add(articleMeta);
						tmp.put(month, ms);
					}else{
						Map<String, List<ArticleMeta>> tmp = sortedMetas.get(year);
						List<ArticleMeta> ms = tmp.get(month);
						if( !ms.contains(articleMeta) ){
							ms.add(articleMeta);
						}
						tmp.put(month, ms);
					}
				}else{
					Map<String,List<ArticleMeta>> tmp = Maps.newLinkedHashMap();
					List<ArticleMeta> ms = Lists.newArrayList();
					ms.add(articleMeta);
					tmp.put(month,ms);
					sortedMetas.put(year, tmp);
				}
		}
		return sortedMetas;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public int getOps() {
		return ops;
	}

	public void setOps(int ops) {
		this.ops = ops;
	}
	
}
